# coding: utf-8
# author: t_wolf
# 公共装饰器方法

from comm_utils import *


# 为方法设置执行延迟,返回一个Timer方法
# 被装饰器处理的方法,需要使用.start()启动原方法执行
def set_timeout(seconds):
    def get_func(func):
        def _func(*args, **kwargs):
            return Timer(seconds, func, args, kwargs)

        return _func

    return get_func


def set_interval(seconds):
    def get_func(func):
        def _func(*args, **kwargs):
            func(*args, **kwargs)

        return _func

    return get_func


def override(func):
    """
    A decorator
    :param func: function
    :return: None
    """

    def on_call(self, *args, **kwargs):
        raise RuntimeError(
            "%s at [%s.%s] must be override." % (
                get_abs_path(CALL_FILE_POSITION), self.__class__.__name__, func.__name__
            )
        )

    return on_call


def runtime_statistic(func):
    """
    this is an Decorator.
    :param func: function object.
    :return: a dict has two key: 'result' is return by function, and 'runtime' is an statistic time.
    """
    def _runtime_get(*arg1, **arg2):
        _start = time.clock()
        return {
            'result': func(*arg1, **arg2),
            'runtime': (time.clock() - _start)
        }
    return _runtime_get
